package com.portal.common.utils;

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Component;

/**
 * 国际化工具类
 *
 * @author Amadeus
 * @date 2024/04/30
 */
@Slf4j
@Component
public class I18nUtil {

    private static MessageSource messageSource = null;

    public I18nUtil(MessageSource messageSource) {
        I18nUtil.messageSource = messageSource;
    }

    /**
     * 获取国际化翻译值
     */
    public static String get(String msgKey) {
        log.info("I18 key:{}, Locale:{}", msgKey, LocaleContextHolder.getLocale());
        return messageSource.getMessage(msgKey, null, LocaleContextHolder.getLocale());
    }

    /**
     * 获取国际化翻译值（包括占位符）
     */
    public String get(String msgKey, Object... args) {
        log.info("I18 key:{}, Locale:{}", msgKey, LocaleContextHolder.getLocale());
        return messageSource.getMessage(msgKey, args, LocaleContextHolder.getLocale());
    }

    public static void main(String[] args) {
        System.out.println(I18nUtil.get("A0503"));
    }

}
